package com.pzh.guli.service.sms.controller;

import cn.pzh.guli.service.utils.PhoneFormatCheckUtils;
import com.pzh.guli.service.sms.service.SmsService;
import cn.pzh.guli.service.utils.RandomNumberSix;
import com.aliyuncs.exceptions.ClientException;
import com.pzh.guli.service.base.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/sms/")
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("send/{tel}")
    public Result sendSms(@PathVariable String tel) throws ClientException {
        //手机号校验
        if(!PhoneFormatCheckUtils.isPhoneLegal(tel)){
            return Result.error().message("手机号格式错误，请检查重试");
        }
        String number = RandomNumberSix.getNumber();
        redisTemplate.opsForValue().set(tel,number,5, TimeUnit.MINUTES);//验证码 手机号存入redis
        smsService.send(tel,number);//发送验证码
        return Result.ok().message("短信发送成功，请注意查收");
    }
}
